/**
 * Created by dukes on 2015/12/4.
 */
//module game {
    class PersonalChatScene extends eui.Component {
        public chater;
        public constructor() {
            super();
            this.skinName = "pet.PersonalChatSceneSkin";
            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAdded, this);
            this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoved, this);
        }

        public updateChater(chater){
            this.chater = chater;
            this.lab_secondary_title.text = "与【"+LimitStringLen(chater.name||chater.userName)+"】正在聊天";
            this.updateChatInfo();
        }

        public addChatItem(data){
            var item;
            if( data.userId != 0){
                item = ChatMgr.getInstance().getHisChatItem();
            }else{
                item = ChatMgr.getInstance().getMyChatItem();
            }
            item.setData(data);
            item.y = this.item_group.numElements * 75;
            this.item_group.addChild(item);
            var sv = this.item_group.contentHeight - 590+75;
            if( sv > 0){
                // this.item_group.verticalScrollPosition = sv;
                // this.scroller._updateContentPosition();
            }
        }

        public updateChatInfo(){
            var chatList:any[] = ChatMgr.getInstance().getChatInfoByUser(this.chater.userId);
            var element;
            while( this.item_group.numElements){
                element = this.item_group.removeChildAt(0);
                ChatMgr.getInstance().reclaimChatItem(element);
            }
            var len = chatList.length;
            var data;
            var item;
            for(var i = 0; i < len; i++){
                data = chatList[i];
                if( data.userId != 0){
                    item = ChatMgr.getInstance().getHisChatItem();
                }else{
                    item = ChatMgr.getInstance().getMyChatItem();
                }
                item.y = 75 * this.item_group.numElements;
                this.item_group.addChild(item);
                item.setData(data);
            }
            var sv = this.item_group.contentHeight - 590;
            if( sv > 0){
                // this.item_group.verticalScrollPosition = sv;
                // this.scroller._updateContentPosition();
            }
        }

        private onVisit(evt:egret.TouchEvent){
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            SceneMgr.getInstance().switchScene(SceneMgr.SCENE_GETOUT,this.chater);
        }

        private onBlack(evt:egret.TouchEvent){
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            window["__block"](this.chater.userId);
        }
        private onReturn(evt:egret.TouchEvent){
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            SceneMgr.getInstance().switchScene(SceneMgr.SCENE_CHAT);
        }

        private onSend(evt:egret.TouchEvent){
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            var text:string = this.input_msg.text.trim();
            if( text.length != 0){
                window["__sendToUser"](this.chater.userId, text);
                this.input_msg.text = "";
                ChatMgr.getInstance().addChatList(this.chater);
                ChatMgr.getInstance().addChatToUser(text,this.chater.userId,0,Model.SYSTIME);
            }
        }
        private lab_secondary_title:eui.Label;
        private img_return:eui.Image;
        private img_send:eui.Image;
        private input_msg:eui.TextInput;
        private img_visit:eui.Image;
        private img_black:eui.Image;
        private item_group:eui.Group;
        private scroller:eui.Scroller;
        public partAdded(name:string, instance:any) {
            super.partAdded(name, instance);
            if (name == "img_return") {
                this.img_return = instance;
            } else if (name == "img_send") {
                this.img_send = instance;
            } else if (name == "input_msg") {
                this.input_msg = instance;
            } else if (name == "img_visit") {
                this.img_visit = instance;
            } else if (name == "img_black") {
                this.img_black = instance;
            } else if (name == "item_group") {
                this.item_group = instance;
            } else if (name == "scroller") {
                this.scroller = instance;
            }else if (name == "lab_secondary_title") {
                this.lab_secondary_title = instance;
            }
        }
        private onAdded(evt:egret.Event){
            this.img_visit.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onVisit, this);
            this.img_return.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onReturn, this);
            this.img_send.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSend, this);
            this.img_black.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBlack, this);
        }

        private onRemoved(evt:egret.Event){
            this.img_visit.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onVisit, this);
            this.img_return.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onReturn, this);
            this.img_send.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onSend, this);
            this.img_black.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onBlack, this);
        }

        private static _instance:PersonalChatScene;
        public static getInstance():PersonalChatScene{
            if( PersonalChatScene._instance == null)
                PersonalChatScene._instance = new PersonalChatScene();
            return PersonalChatScene._instance;
        }
    }
//}